package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.pojo.dto.DistributorMessageRabbitMqDTO;
import com.xbongbong.pro.message.pojo.vo.MessageRabbitMqVO;
import com.xbongbong.pro.message.service.DistributorMessageService;
import com.xbongbong.pro.message.service.MessageService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @Description 对账单消息处理接口
 * @author: shian
 * @date : 2021/2/22
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/message/mq/statement")
public class MessageRabbitmqStatementController {

    @Resource
    private MessageService messageService;
    @Resource
    private DistributorMessageService distributorMessageService;

    /**
     * 对账单生成
     * @param messageRabbitMqDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/generate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String generate(@RequestBody @Valid DistributorMessageRabbitMqDTO messageRabbitMqDTO, BindingResult br) throws Exception{
        XbbResponse<MessageRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if (Objects.equals(messageRabbitMqDTO.getDistributorMark(), DistributorMarkEnum.DISTRIBUTOR.getCode())) {
                    distributorMessageService.pushDistributorMessage(messageRabbitMqDTO, PushTypeEnum.STATEMENT_GENERATE_PUSH);
                }else {
                    messageService.delayPushDingtalkOAMessage(messageRabbitMqDTO, PushTypeEnum.STATEMENT_GENERATE_PUSH);
                }
                MessageRabbitMqVO messageRabbitMqVO = new MessageRabbitMqVO();
                response = new XbbResponse<>(messageRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 对账单提醒确认
     * @param messageRabbitMqDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/remindConfirm", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String remindConfirm(@RequestBody @Valid DistributorMessageRabbitMqDTO messageRabbitMqDTO, BindingResult br) throws Exception{
        XbbResponse<MessageRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                distributorMessageService.pushDistributorMessage(messageRabbitMqDTO, PushTypeEnum.STATEMENT_REMIND_CONFIRM_PUSH);
                MessageRabbitMqVO messageRabbitMqVO = new MessageRabbitMqVO();
                response = new XbbResponse<>(messageRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 对账单代客确认
     * @param messageRabbitMqDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/valetConfirm", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String valetConfirm(@RequestBody @Valid DistributorMessageRabbitMqDTO messageRabbitMqDTO, BindingResult br) throws Exception{
        XbbResponse<MessageRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                distributorMessageService.pushDistributorMessage(messageRabbitMqDTO, PushTypeEnum.STATEMENT_VALET_CONFIRM_PUSH);
                MessageRabbitMqVO messageRabbitMqVO = new MessageRabbitMqVO();
                response = new XbbResponse<>(messageRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 对账单重新生成
     * @param messageRabbitMqDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/regenerate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String regenerate(@RequestBody @Valid DistributorMessageRabbitMqDTO messageRabbitMqDTO, BindingResult br) throws Exception{
        XbbResponse<MessageRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                distributorMessageService.pushDistributorMessage(messageRabbitMqDTO, PushTypeEnum.STATEMENT_REGENERATE_PUSH);
                MessageRabbitMqVO messageRabbitMqVO = new MessageRabbitMqVO();
                response = new XbbResponse<>(messageRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 对账单疑问反馈
     * @param messageRabbitMqDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/questionFeedback", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String questionFeedback(@RequestBody @Valid DistributorMessageRabbitMqDTO messageRabbitMqDTO, BindingResult br) throws Exception{
        XbbResponse<MessageRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                messageService.delayPushDingtalkOAMessage(messageRabbitMqDTO, PushTypeEnum.STATEMENT_QUESTION_FEEDBACK_PUSH);
                MessageRabbitMqVO messageRabbitMqVO = new MessageRabbitMqVO();
                response = new XbbResponse<>(messageRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
